package com.ad.pet.common.data.utils.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by z on.
 * Des:时间转换器工具类
 * Date: 2017/3/17
 */

public class DateUtil {


    /**
     * 将时间转换为时间戳
     *
     * @param s
     * @return
     */
    public static Long dateToStamp(String s) {
        long res = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = ts;
        } catch (ParseException e) {
            e.printStackTrace();

        }
        return res;
    }


    /**
     * 将时间戳转换为时间
     *
     * @param s
     * @return
     */
    public static String stampToDate(Long s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(s);
        String res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为自定义格式时间
     *
     * @param s
     * @param format
     * @return
     */
    public static String stampToDate(Long s, String format) {
        if (s == null) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date(s);
        String res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToDate(Date date, String format) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String res = simpleDateFormat.format(date);
        return res;
    }

    public static String getWeekOfDate(long dt) {
        Date date2 = new Date(dt);
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date2);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }

    public static String getWeekPosition(
            String week
    ) {
        String[] weekDays = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六","星期日",};
        int i0 = 0;
        for (int i = 0; i < weekDays.length; i++) {
            if (weekDays[i].equals(week)) {
                i0 = i;
                break;
            }
        }
        return String.valueOf(i0+1);
    }

}
